<template>
  <div class="page">
    <h2>输入 Git 仓库链接</h2>
    <el-input v-model="gitLink" placeholder="请输入 Git 仓库 URL" clearable></el-input>
    <el-button type="primary" @click="submitLink" :loading="loading">
      <span v-if="!loading">提交</span>
      <span v-else>加载中...</span>
    </el-button>

    <!-- 下载成功后的提示框 -->
    <transition name="fade">
      <div v-if="downloadSuccess" class="success-animation">
       生成成功！
      </div>
    </transition>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { useGitStore } from '@/stores/gitStore';
import { ElLoading } from 'element-plus'; // 只保留加载组件

export default defineComponent({
  setup() {
    const gitLink = ref(''); // 输入框绑定的值
    const loading = ref(false); // 按钮的加载状态
    const downloadSuccess = ref(false); // 下载成功状态
    const gitStore = useGitStore(); // 获取 store 实例

    const submitLink = async () => {
      loading.value = true; // 点击后设置加载状态为 true

      // 显示 loading 动画，模拟等待 15 秒
      const loadingInstance = ElLoading.service({
        lock: true,
        text: '处理中，请稍候...',
        background: 'rgba(0, 0, 0, 0.7)',
      });

      setTimeout(async () => {
        try {
          await gitStore.fetchGitData(gitLink.value); // 调用 store 中的函数
          downloadSuccess.value = true; // 设置下载成功状态

          // 2 秒后隐藏下载成功提示
          setTimeout(() => {
            downloadSuccess.value = false;
          }, 2000);
        } finally {
          loading.value = false; // 请求完成后，无论成功与否，重置加载状态
          loadingInstance.close(); // 关闭加载动画
        }
      }, 15000); // 等待 15 秒后执行逻辑
    };

    return {
      gitLink,
      loading,
      submitLink,
      downloadSuccess,
    };
  },
});
</script>

<style scoped>
.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  background-color: #f9f9f9; /* 增加背景色 */
  border-radius: 8px; /* 添加圆角效果 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
}

h2 {
  margin-bottom: 20px;
  font-size: 1.8rem;
  color: #333; /* 改善字体颜色 */
}

.el-input {
  width: 100%;
  max-width: 400px;
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
  max-width: 150px;
}

.el-button[type="primary"] {
  background-color: #337ab7;
  border-color: #337ab7;
  color: #fff;
}

.el-button[type="primary"]:hover {
  background-color: #285e8e;
  border-color: #285e8e;
}

/* 下载成功动画 */
.success-animation {
  margin-top: 20px;
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border-radius: 4px;
  font-size: 1.2rem;
  transition: opacity 0.5s ease-in-out;
}

/* 淡入淡出效果 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
  opacity: 0;
}
</style>